import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Button,
} from 'antd';
import StandardTable from '@/components/StandardTable';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import DescView from './descView';
import styles from '../UserManage.less';
import { powerBtnFunc } from '@/utils/utils';
import InviteList from '../Platuser/inviteList';

const FormItem = Form.Item;

/* eslint react/no-multi-comp:0 */
@connect(({ Invite, loading }) => ({
  Invite,
  loading: loading.models.Invite,
}))

@Form.create()
class Invite extends PureComponent {
  state = {
    modalVisible: false,
    updateModalVisible: false,
    lookDescView: false,
    selectedRows: {},
    formValues: {},
    pagination: {
      pageNo: 1,
      pageSize: 15,
    },
  };

  columns = [
    {
      title: '邀请人手机号',
      dataIndex: 'mobile',
    },
    {
      title: '邀请人数',
      render: (text, record) => (
        <a onClick={() => this.handleLookDesc(true, record)}>{record.inviteNum}</a>
      ),
    },
    {
      title: '注册时间',
      dataIndex: 'regTime',
    },
    {
      title: '实名人数',
      dataIndex: 'accountNum',
    },
    {
      title: '出借人数',
      dataIndex: 'borrowNum',
    },
    {
      title: '操作',
      render: (text, record) => (
        <Fragment>
          {
            powerBtnFunc('100120',
              <a key={'btn'} onClick={() => this.handleLookDesc(true, record)}>查看受邀人</a>,
            )}
        </Fragment>
      ),
    },
  ];

  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'Invite/fetch',
    });
  }

  handleStandardTableChange = (pagination, filtersArg, sorter) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;


    this.setState({
      pagination: {
        pageNo: pagination.current,
        pageSize: 15,
      },
    });
    const params = {
      pageNo: pagination.current,
      pageSize: 15,
      ...formValues,
    };

    dispatch({
      type: 'Invite/fetch',
      payload: params,
    });
  };
  //重置
  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      formValues: {},
    });
    dispatch({
      type: 'Invite/fetch',
      payload: {},
    });
  };


  handleSelectRows = rows => {
    this.setState({
      selectedRows: rows,
    });
  };
  //搜索
  handleSearch = e => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      const values = {
        ...fieldsValue,
      };
      this.setState({
        formValues: values,
      });

      dispatch({
        type: 'Invite/fetch',
        payload: values,
      });
    });
  };

  //查看
  handleLookDesc = (flag, record) => {
    const { dispatch } = this.props;
    this.setState({
      lookDescView: !!flag,
      selectedRows: record,
    }, () => {
      if (!!flag) {
        dispatch({
          type: 'Invite/detail',
          payload: { userNo: record.userNo },
        });
      }
    });

  };


  renderSimpleForm() {
    const {
      form: { getFieldDecorator },
    } = this.props;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={6} sm={24}>
            <FormItem label="手机号">
              {getFieldDecorator('mobile')(<Input placeholder="请输入"/>)}
            </FormItem>
          </Col>

          <Col md={6} sm={24}>
        <span className={styles.submitButtons}>
        <Button type="primary" htmlType="submit">
        查询
        </Button>
        <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
        重置
        </Button>

        </span>
          </Col>
        </Row>
      </Form>
    );
  }


  render() {
    const { Invite: { data, details }, loading } = this.props;
    const { selectedRows, lookDescView } = this.state;
    return (
      <PageHeaderWrapper title="邀请管理">
        <Card bordered={false}>

          <div className={styles.tableList}>
            <div className={styles.tableListForm}>{this.renderSimpleForm()}</div>

            <StandardTable
              selectedRows={selectedRows}
              loading={loading}
              data={data ? data : []}
              rowKey='userNo'
              rowSelectHide={true}
              columns={this.columns}
              onSelectRow={this.handleSelectRows}
              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>
        <DescView
          userList={details ? details : {}}
          handleLookDesc={this.handleLookDesc}
          selectedRows={selectedRows}
          lookDescView={lookDescView}/>
      </PageHeaderWrapper>
    );
  }
}

export default Invite;
